package com.shuda.business.user.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.shuda.business.user.entity.TenantEntity;
import com.shuda.business.user.service.TenantService;
import com.shuda.common.dto.PageDTO;
import com.shuda.common.utils.PageResult;
import com.shuda.common.result.R;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import jakarta.validation.Valid;
import java.util.List;

/**
 * 租户管理控制器类
 */
@RestController
@RequestMapping("/api/system/tenant")
@Tag(name = "系统租户管理", description = "系统租户管理相关接口")
@RequiredArgsConstructor
@Validated
public class TenantController {

    private final TenantService tenantService;

    /**
     * 分页查询租户列表
     */
    @GetMapping("/page")
    @Operation(summary = "分页查询租户列表")
    /**
     *
     * 分页查询租户列表
     * @param pageDTO 分页参数
     * @param tenantName 租户名称
     * @param tenantCode 租户编码
     * @param status 状态
     * @return 分页结果
     * @author ink
     * @Description: 统一为 com.shuda.common.dto.PageDTO、com.shuda.common.utils.PageResult、com.shuda.common.result.R
     * @date 2025-11-10 00:22
     */
    public R<PageResult<TenantEntity>> getTenantPage(@Valid PageDTO pageDTO,
                                                         @RequestParam(required = false) String tenantName,
                                                         @RequestParam(required = false) String tenantCode,
                                                         @RequestParam(required = false) String status) {
        QueryWrapper<TenantEntity> queryWrapper = new QueryWrapper<>();
        if (tenantName != null && !tenantName.trim().isEmpty()) {
            queryWrapper.like("tenant_name", tenantName);
        }
        if (tenantCode != null && !tenantCode.trim().isEmpty()) {
            queryWrapper.like("tenant_code", tenantCode);
        }
        if (status != null && !status.trim().isEmpty()) {
            queryWrapper.eq("status", status);
        }
        queryWrapper.orderByDesc("create_time");
        
        PageResult<TenantEntity> pageResult = tenantService.getTenantPage(pageDTO, queryWrapper);
        return R.ok(pageResult);
    }

    /**
     * 获取租户详情
     */
    @GetMapping("/{id}")
    @Operation(summary = "获取租户详情")
    public R<TenantEntity> getTenantById(@PathVariable Long id) {
        TenantEntity tenant = tenantService.getById(id);
        return R.ok(tenant);
    }

    /**
     * 创建租户
     */
    @PostMapping
    @Operation(summary = "创建租户")
    public R<String> createTenant(@Valid @RequestBody TenantEntity tenant) {
        tenantService.createTenant(tenant);
        return R.ok("租户创建成功");
    }

    /**
     * 更新租户
     */
    @PutMapping("/{id}")
    @Operation(summary = "更新租户")
    public R<String> updateTenant(@PathVariable Long id, @Valid @RequestBody TenantEntity tenant) {
        tenant.setId(id);
        tenantService.updateTenant(tenant);
        return R.ok("租户更新成功");
    }

    /**
     * 删除租户
     */
    @DeleteMapping("/{id}")
    @Operation(summary = "删除租户")
    public R<String> deleteTenant(@PathVariable Long id) {
        tenantService.deleteTenant(id);
        return R.ok("租户删除成功");
    }

    /**
     * 批量删除租户
     */
    @DeleteMapping("/batch")
    @Operation(summary = "批量删除租户")
    public R<String> deleteTenants(@RequestBody List<Long> ids) {
        tenantService.deleteTenants(ids);
        return R.ok("租户批量删除成功");
    }

    /**
     * 更新租户状态
     */
    @PutMapping("/{id}/status")
    @Operation(summary = "更新租户状态")
    public R<String> updateTenantStatus(@PathVariable Long id, @RequestParam String status) {
        tenantService.updateTenantStatus(id, status);
        return R.ok("租户状态更新成功");
    }

    /**
     * 获取租户配置
     */
    @GetMapping("/{id}/config")
    @Operation(summary = "获取租户配置")
    public R<TenantEntity> getTenantConfig(@PathVariable Long id) {
        TenantEntity tenant = tenantService.getTenantConfig(id);
        return R.ok(tenant);
    }

    /**
     * 更新租户配置
     */
    @PutMapping("/{id}/config")
    @Operation(summary = "更新租户配置")
    public R<String> updateTenantConfig(@PathVariable Long id, @RequestBody TenantEntity config) {
        tenantService.updateTenantConfig(id, config);
        return R.ok("租户配置更新成功");
    }
}